package contentservicelogic

import (
	"center-data-platform/app/content/model"
	"center-data-platform/app/content/rpc/content"
	"center-data-platform/app/content/rpc/internal/svc"
	user2 "center-data-platform/app/user/rpc/user"
	"context"
	"strings"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailLogic {
	return &DetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DetailLogic) Detail(in *content.PostDetailReq) (*content.PostDetailResp, error) {
	var contents model.Content
	err := l.svcCtx.DB.Model(&model.Content{}).Where("id = ?", in.Id).First(&contents).Error
	if err != nil {
		return nil, err
	}
	tags := strings.Split(contents.Tags, ",")
	user, err := l.svcCtx.UserRpc.Info(l.ctx, &user2.UserInfoReq{Id: contents.UserId})
	if err != nil {
		return nil, err
	}
	return &content.PostDetailResp{
		Code: 200,
		Msg:  "请求成功",
		Data: &content.PostDetail{
			Id:          contents.Id,
			Title:       contents.Title,
			Content:     contents.Content,
			Description: contents.Description,
			Cover:       contents.Cover,
			Tags:        tags,
			Type:        contents.Type,
			Status:      contents.Status,
			CreatedAt:   contents.CreatedTime.Format("2006-01-02 15:04:05"),
			UpdatedAt:   contents.UpdatedTime.Format("2006-01-02 15:04:05"),
			UserId:      user.GetId(),
			UserName:    user.GetUserName(),
		},
	}, nil
}
